package com.jzh.selfwallpaper.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jzh.selfwallpaper.R;
import com.jzh.selfwallpaper.view.fragment.Fragment_active;
import com.jzh.selfwallpaper.view.fragment.Fragment_home;
import com.jzh.selfwallpaper.view.fragment.Fragment_my;

public class IndexActivity extends BaseActivity {


    private LinearLayout ll_home, ll_active, ll_my;
    private ImageView iv_home, iv_message, iv_mine,iv_topbar_back;
    private TextView tv_topbar_title;
    private FragmentManager fm;
    private Fragment fragment_home, fragment_active, fragment_my;

    @Override
    public void widgetClick(View v) {

        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch (v.getId()) {
            case R.id.ll_active:
                tv_topbar_title.setText(getResources().getString(R.string.active));
                iv_home.setBackgroundResource(R.drawable.index);
                iv_message.setBackgroundResource(R.drawable.active_fill);
                iv_mine.setBackgroundResource(R.drawable.my);
                hideFragment(fragment_home, fragmentTransaction);
                hideFragment(fragment_my, fragmentTransaction);
                //将homefragment显示到用户面前
                if (fragment_active == null) {
                    fragment_active = new Fragment_active();
                    fragmentTransaction.add(R.id.content_layout, fragment_active);
                } else {
                    fragmentTransaction.show(fragment_active);
                }
                break;
            case R.id.ll_home:
                tv_topbar_title.setText(getResources().getString(R.string.index));
                iv_home.setBackgroundResource(R.drawable.index_fill);
                iv_message.setBackgroundResource(R.drawable.active);
                iv_mine.setBackgroundResource(R.drawable.my);
                hideFragment(fragment_active, fragmentTransaction);
                hideFragment(fragment_my, fragmentTransaction);
                //将homefragment显示到用户面前
                if (fragment_home == null) {
                    fragment_home = new Fragment_home();
                    fragmentTransaction.add(R.id.content_layout, fragment_home);
                } else {
                    fragmentTransaction.show(fragment_home);
                }
                break;
            case R.id.ll_my:
                tv_topbar_title.setText(getResources().getString(R.string.my));
                iv_home.setBackgroundResource(R.drawable.index);
                iv_message.setBackgroundResource(R.drawable.active);
                iv_mine.setBackgroundResource(R.drawable.my_fill);
                hideFragment(fragment_active, fragmentTransaction);
                hideFragment(fragment_home, fragmentTransaction);
                //将homefragment显示到用户面前
                if (fragment_my == null) {
                    fragment_my = new Fragment_my();
                    fragmentTransaction.add(R.id.content_layout, fragment_my);
                } else {
                    fragmentTransaction.show(fragment_my);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    protected void init() {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_index;
    }

    @Override
    public void initView(View view) {

        ll_active = findViewById(R.id.ll_active);
        ll_home = findViewById(R.id.ll_home);
        ll_my = findViewById(R.id.ll_my);
        iv_home = findViewById(R.id.iv_home);
        iv_home.setBackgroundResource(R.drawable.index_fill);
        iv_message = findViewById(R.id.iv_message);
        iv_message.setBackgroundResource(R.drawable.active);
        iv_mine = findViewById(R.id.iv_mine);
        iv_mine.setBackgroundResource(R.drawable.my);
        iv_topbar_back=findViewById(R.id.iv_topbar_back);
        iv_topbar_back.setVisibility(View.GONE);
        tv_topbar_title=findViewById(R.id.tv_topbar_title);
    }

    @Override
    public void setListener() {
        ll_active.setOnClickListener(this);
        ll_my.setOnClickListener(this);
        ll_home.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {

        //添加要显示的默认的Fragent
        fragment_home = new Fragment_home();
        fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.content_layout, fragment_home);
        fragmentTransaction.commit();
    }

    /**
     * 用来隐藏具体的Fragment
     *
     * @param fragment
     * @param fragmentTransaction
     */
    private void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {

        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }
}
